package game.gui;

import game.Game;
import game.MapShareWindow;
import game.event.ChatEvent;

import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;

public class GameMainGUI implements MapShareWindow {

	private Shell sShell = null;
	private SashForm sashFormVertical = null;
	private SashForm sashFormHorizontal = null;
	private List playerList = null;
	private Canvas mapCanvas = null;
	private SashForm chatSashForm = null;
	private Text chatHistoryTextArea = null;
	private Text chatInputTextArea = null;
	private Game game;
	public GameMainGUI(Game game){
		this.game = game;
		this.game.setGUI(this);
		createSShell();
		sShell.open();
	}
	
	/**
	 * This method initializes sShell
	 */
	private void createSShell() {
		FillLayout fillLayout = new FillLayout();
		fillLayout.type = org.eclipse.swt.SWT.VERTICAL;
		sShell = new Shell();
		sShell.setText("MapShare");
		createSashFormVertical();
		sShell.setLayout(fillLayout);
		sShell.setSize(new Point(567, 616));
	}

	@Override
	public Shell getMainShell() {
		return sShell;
	}

	/**
	 * This method initializes sashFormVertical	
	 *
	 */
	private void createSashFormVertical() {
		sashFormVertical = new SashForm(sShell, SWT.NONE);
		sashFormVertical.setOrientation(SWT.VERTICAL);
		createSashFormHorizontal();
		createChatSashForm();
		sashFormVertical.setWeights(new int[]{3,1});
	}

	/**
	 * This method initializes sashFormHorizontal	
	 *
	 */
	private void createSashFormHorizontal() {
		sashFormHorizontal = new SashForm(sashFormVertical, SWT.NONE);
		createMapCanvas();
		playerList = new List(sashFormHorizontal, SWT.BORDER);
		playerList.setToolTipText("List of connected participants");
		sashFormHorizontal.setWeights(new int[]{3,1});
	}

	/**
	 * This method initializes mapCanvas	
	 *
	 */
	private void createMapCanvas() {
		mapCanvas = new Canvas(sashFormHorizontal, SWT.BORDER);
	}

	/**
	 * This method initializes chatSashForm	
	 *
	 */
	private void createChatSashForm() {
		chatSashForm = new SashForm(sashFormVertical, SWT.NONE);
		chatSashForm.setOrientation(SWT.VERTICAL);
		chatHistoryTextArea = new Text(chatSashForm, SWT.MULTI | SWT.WRAP | SWT.V_SCROLL | SWT.BORDER);
		chatHistoryTextArea.setEditable(false);
		chatHistoryTextArea.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_LIST_BACKGROUND));
		chatHistoryTextArea.setToolTipText("Chat history");
		chatInputTextArea = new Text(chatSashForm, SWT.MULTI | SWT.WRAP | SWT.V_SCROLL);
		chatInputTextArea.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent e) {
				if(e.keyCode == SWT.CR){
					game.performLocalChat(chatInputTextArea.getText());
					chatInputTextArea.setText("");
				}
			}
		});
		chatInputTextArea.setToolTipText("Type here, hit enter when you're ready to send");
		chatSashForm.setWeights(new int[]{3,1});
	}

	public void displayMessage(ChatEvent chatEvent) {
		chatHistoryTextArea.append(chatEvent.getFormattedMessage());
	}

}